<?php
class SanphamController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
        $this->view->headScript()->appendFile('path/to/the/javascript/file');
    }

    public function indexAction()
    {
        // action body
    }

    public function taoSanPhamAction()
    {
        // action body
            $name = array ();
            $tenSanPham = $this->_request->getParam ( 'tenSanPham' );
            $maSanPham = $this->_request->getParam ( 'maSanPham' );
            $idDanhMuc= $this->_request->getParam ( 'danhMuc' );
            $moTa= $this->_request->getParam ( 'moTa' );
            $giaGoc= $this->_request->getParam ( 'giaGoc' );
            $giaBan= $this->_request->getParam ( 'giaBan' );
            $chucNang= $this->_request->getParam ( 'chucNang' );
            $chucNangKhac= $this->_request->getParam ( 'chucNangKhac' );
            $tinhTrang= $this->_request->getParam ( 'tinhTrang' );
            $name = Application_Model_util_MyImageUtil::makeUpload ( 'sanPham', $this->getRequest (), 600, 600 );
            
            //goi service de luu
            $sv=Application_Model_service_SanPhamService::getInstance();
            $result=$sv->taoSanPham($tenSanPham, $maSanPham, $idDanhMuc, $giaGoc, $giaBan, $moTa, $chucNang, $chucNangKhac, $name,$tinhTrang);
            //add view
            $this->view->result=$result;
            $this->_helper->redirector('quanlysanpham');
    }

    public function newAction()
    {
        // action body
            //lay danh muc san pham
            $sv=Application_Model_service_DanhMucService::getInstance();
            $listDanhMuc=$sv->layDanhSachDanhMucSanPham();
            //lay danh sach chuc nang
            $sv=Application_Model_service_ChucNangSanPhamService::getInstance();
            $listChucNang=$sv->loadAllItemChucNangSanPham();
            //add view
            $this->view->danhMuc=$listDanhMuc;
            $this->view->chucNang=$listChucNang;
    }

    public function loadAction()
    {
        // action body
            $id=$this->_request->getParam('id');
            //goi service
            $service=Application_Model_service_SanPhamService::getInstance();
            $service instanceof Application_Model_serviceInf_SanPhamServiceInf;
            $result=$service->loadSanPham($id);
            
            //add view
            $this->view->sanPham=$result;
    }

    public function getListAction()
    {
    	$ajax=$this->_request->getParam('ajax');
        // action body
            $limit=$this->_request->getParam('lm');
            $page=$this->_request->getParam('pa');
            $tenSanPham=$this->_request->getParam('ten');
            $maDanhMuc=$this->_request->getParam('maDM');
            $giaBan=$this->_request->getParam('g');
            $maChucNang=$this->_request->getParam('mcn');
            //goi service
            $service=Application_Model_service_SanPhamService::getInstance();
            $service instanceof Application_Model_serviceInf_SanPhamServiceInf;
            $arraySanPham=$service->getListSanPham($limit,$page,$tenSanPham,$maDanhMuc,$giaBan,$maChucNang);
            $pageNumber=$service->pages($limit,$page,$tenSanPham,$maDanhMuc,$giaBan,$maChucNang);
            //add view
            $this->view->listSanPham=$arraySanPham;
            $this->view->pageNumber=$pageNumber;
            $this->view->limit=$limit;
            $this->view->page=$page;
          if($ajax!="") $this->_helper->layout()->disableLayout();
    }

    public function xoaAction()
    {
        // action body
    }

    public function loadbydanhmucAction()
    {
        // action body
            //goi service
            $limit=$this->_request->getParam('lm');
            $page=$this->_request->getParam('pa');
            $tenSanPham="";
            $maDanhMuc=$this->_request->getParam('maDM');
            $giaBan=0;
            $maChucNang="";
            //goi service
            $serviceDM=Application_Model_service_DanhMucService::getInstance();
            $serviceDM instanceof Application_Model_serviceInf_DanhMucServiceInf;
            
            $danhMuc=$serviceDM->loadMotDanhMucSanPham($maDanhMuc);
            
            $service=Application_Model_service_SanPhamService::getInstance();
            $service instanceof Application_Model_serviceInf_SanPhamServiceInf;
            $arraySanPham=$service->getListSanPham($limit,$page,$tenSanPham,$maDanhMuc,$giaBan,$maChucNang);
            $pageNumber=$service->pages($limit,$page,$tenSanPham,$maDanhMuc,$giaBan,$maChucNang);
            //add view
            $this->view->listSanPham=$arraySanPham;
            $this->view->danhMuc=$danhMuc;
            $this->view->pageNumber=$pageNumber;
            $this->view->page=$page;
            $this->view->limit=$limit;
    }

    public function updateHinhAction()
    {
        // action body
    }

    public function updateHinhSuccessAction()
    {
        // action body
    }

    public function timnhanhAction()
    {
        // action body
            $limit=$this->_request->getParam('lm');
            $page=$this->_request->getParam('pa');
            $tenSanPham=$this->_request->getParam('ten');
            $maDanhMuc="";
            $giaBan=0;
            $maChucNang="";
            //goi service
            $service=Application_Model_service_SanPhamService::getInstance();
            $service instanceof Application_Model_serviceInf_SanPhamServiceInf;
            $arraySanPham=$service->getListSanPham($limit,$page,$tenSanPham,$maDanhMuc,$giaBan,$maChucNang);
            $pageNumber=$service->pages($limit,$page,$tenSanPham,$maDanhMuc,$giaBan,$maChucNang);
            //add view
            $this->view->listSanPham=$arraySanPham;
            $this->view->pageNumber=$pageNumber;
            $this->view->tenSP=$tenSanPham;
            $this->view->page=$page;
            $this->view->limit=$limit;
    }

    public function viewajaxAction()
    {
        // action body
            $id=$this->_request->getParam('id');
            //goi service
            $service=Application_Model_service_SanPhamService::getInstance();
            $service instanceof Application_Model_serviceInf_SanPhamServiceInf;
            $result=$service->loadSanPham($id);
            
            //add view
            $this->view->sanPham=$result;
            $this->_helper->layout()->disableLayout();
    }

    public function getonpageAction()
    {
        // action body
                                                                                 $limit=$this->_request->getParam('lm');
            $page=$this->_request->getParam('pa');
            $tenSanPham=$this->_request->getParam('ten');
            $maDanhMuc=$this->_request->getParam('maDM');
            $giaBan=$this->_request->getParam('g');
            $maChucNang=$this->_request->getParam('mcn');
            //goi service
            $service=Application_Model_service_SanPhamService::getInstance();
            $service instanceof Application_Model_serviceInf_SanPhamServiceInf;
            $arraySanPham=$service->getListSanPham($limit,$page,$tenSanPham,$maDanhMuc,$giaBan,$maChucNang);
            //add view
            $this->view->listSanPham=$arraySanPham;
            $this->_helper->layout()->disableLayout();
    }

    public function updateThongTinChungAction()
    {
        // action body
        $id=$this->_request->getParam('id');
        $control=$this->_request->getParam('ct');
        $action=$this->_request->getParam('act');
        
        //goi serive load san pham
        $serviceSP=Application_Model_service_SanPhamService::getInstance();
        $serviceSP instanceof Application_Model_serviceInf_SanPhamServiceInf;
        $sanPham=$serviceSP->loadSanPham($id);
        
        //lay danh muc san pham
            $sv=Application_Model_service_DanhMucService::getInstance();
            $listDanhMuc=$sv->layDanhSachDanhMucSanPham();
        //add view
        $this->view->sanPham=$sanPham;
        $this->view->control=$control;
        $this->view->action=$action;
        $this->view->listDanhMuc=$listDanhMuc;
        $this->view->id=$id;
    }

    public function updateThongTinChungSuccessAction()
    {
        // action body
        $id=$this->_request->getParam('id');
        $control=$this->_request->getParam('ct');
        $action=$this->_request->getParam('act');
        // action body
            $tenSanPham = $this->_request->getParam ( 'tenSanPham' );
            $idDanhMuc= $this->_request->getParam ( 'danhMuc' );
            $moTa= $this->_request->getParam ( 'moTa' );
            $tinhTrang= $this->_request->getParam ( 'tinhTrang' );
            
            //goi service de luu
            $sv=Application_Model_service_SanPhamService::getInstance();
            $sv instanceof Application_Model_serviceInf_SanPhamServiceInf;
            $result=$sv->updateThongTinChung($id,$tenSanPham,$idDanhMuc,$moTa,$tinhTrang);
            //add view
            $this->view->result=$result;
            $this->_helper->redirector($action);
    }

    public function updateBaiVietAction()
    {
        // action body
    }

    public function updateBaiVietSuccessAction()
    {
        // action body
    }

    public function updateChucNangAction()
    {
        // action body
    }

    public function updateChucNangSuccessAction()
    {
        // action body
    }

    public function updateHinhAnhAction()
    {
        // action body
    }

    public function updateGiaAction()
    {
        // action body
    }

    public function updateGiaSuccessAction()
    {
        // action body
    }

	public function quanlysanphamAction(){
		$ajax=$this->_request->getParam('ajax');
		//lay danh sach san pham 
		$limit=$this->_request->getParam('lm');
        $page=$this->_request->getParam('pa');
        $tenSanPham=$this->_request->getParam('ten');
        $maDanhMuc=$this->_request->getParam('maDM');
        $giaBan=$this->_request->getParam('g');
        $maChucNang=$this->_request->getParam('mcn');
        //goi service
        
        if($giaBan=='')$giaBan=0;
        if($page=='')$page=1;
        if ($limit=='')$limit=10;
        
        $service=Application_Model_service_SanPhamService::getInstance();
        $service instanceof Application_Model_serviceInf_SanPhamServiceInf;
        $arraySanPham=$service->getListSanPham($limit,$page,$tenSanPham,$maDanhMuc,$giaBan,$maChucNang);
        $pageNumber=$service->pages($limit,$page,$tenSanPham,$maDanhMuc,$giaBan,$maChucNang);
        //add view
        $this->view->listSanPham=$arraySanPham;
        $this->view->pageNumber=$pageNumber; 
        if($ajax!='') $this->_helper->layout()->disableLayout();
	}
}























































